Practical No.4: Develop JavaScript to implement functions.

Introduction:

Functions are very important and useful in any programming language as they make the code reusable. A function is a block of code which will be executed only if it is called. If you have a few lines of code that needs to be used several times, you can create a function including the repeating lines of code and then call the function wherever you want.


What is Function?

  A function is a group of reusable code which can be called anywhere in your program.

  This eliminates the need of writing the same code repeatedly.

  It helps programmers in writing modular codes.

  Functions allow a programmer to divide a big program into a number of small and manageable functions.


Declaring A Function:

  Before we use a function, we need to define it. The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.

  Syntax:

function functionname(parameter-list)

{

statements

}

 

Adding An Arguments:

  We can pass some arguments to the function.

  Syntax:

function functionname(argument1, argument2,….. argumentn)

{

statements

}

Returning A Value From A Function:

  A JavaScript function can have an optional return statement.

  This is required if you want to return a value from a function. This statement should be the last statement in a function.

  The return value is either stored in variable or directly display in browser


Conclusion: We understand that how implement functions JavaScript.

 

JavaScript to check vowels from given string-using function.

<html>

<head>

<script type="text/javascript"> var count = 0;

var name = prompt("Please enter your name"); for (var i=0;i<name.length;i++)

{

if(name[i] == "a" || name[i] == "e" || name[i] == "i" || name[i] == "o" || name[i] == "u"){ count = count + 1;

}

}

document.write("Hello " + name + "...!!! Your name has " + count + " vowels.");

</script>

</head>

<body>

</body>

</html>

Output:       Hello abhi...!!! Your name has 2 vowels.


-----------------------------------------------------------------------------------------------------


JavaScript Return Value using function.

             A]

<html>

<head>

<script type="text/javascript"> function returnSum(first, second)

{

            var sum = first + second; return sum;
            }
            var firstNo = 78; var secondNo = 22;

document.write(firstNo + " + " + secondNo + " = " + returnSum(firstNo,secondNo));

</script>

</head>

<body>

</body>

</html>


Output: 78 + 22 = 100

-----------------------------------------------------------------------------------------------------

B]

<html>

<head>

<script type="text/javascript"> function returnSum(first) {

var square = first * first; return square;

}

var firstNo = 16;

document.write("Square" + " = " + returnSum(firstNo));

</script>

</head>

<body>

</body>

</html>

Output:       Square = 256

Comments